package com.gzbit.calculator.ui;

import com.gzbit.calculator.bll.IOperation;
import com.gzbit.calculator.factory.AddFactory;
import com.gzbit.calculator.factory.IFactory;
import com.gzbit.calculator.factory.MulFactory;
import com.gzbit.calculator.factory.SubFactory;
//import jdk.nashorn.internal.parser.Scanner;
import java.util.Scanner;

class Calculate<T extends Number> {
    public void CommonCalculate(T numberA, T numberB,String oprType) throws Exception {     //throws exception 捕获异常

       IOperation<T> opr=null;
       IFactory<T> factory = null;
       T result;
       //根据运算类符号确定实例化具体工厂类对象
       switch(oprType) {
         case "+":
           factory = new AddFactory<T>();
           break;
         case "-":
           factory = new SubFactory<T>();
           break;
         case "*":
           factory = new MulFactory<T>();
         default:
           throw new Exception("输入的运算符号错误！");   //一旦出错，下面的代码就不执行了，不需要多余的break；
       }
        opr=factory.getOperation();
        opr.setValue(numberA,numberB);
        result=opr.getResult();
        System.out.println(result);
    }
}

public class Test{
    public static void main(String[] args)throws Exception{

        //calculate.CommonCalculate(1,2,"+");
        Scanner scanner=new Scanner(System.in);
        String oprtype;
        System.out.println("输入两个整数和运算类型:\n");
        int numberA=scanner.nextInt();
        int numberB=scanner.nextInt();
        oprtype=scanner.next();
        Calculate<Integer> calculate=new Calculate<>();
        calculate.CommonCalculate(numberA,numberB,oprtype);

    }
}
